package com.sq.universe.flowable.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.flowable.entity.FlowFromEntity;
import  com.sq.universe.flowable.service.FlowFromService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@RestController
@RequestMapping("/flowFrom")
@CrossOrigin
public class FlowFromController extends BaseController {

    @Autowired
    private FlowFromService flowFromService;

    /**
    * 查询数据
    * @param flowFromEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("flowable:flowFrom:query")
    public RespEntity query(@RequestBody(required = false) FlowFromEntity flowFromEntity){
        try {
            return new RespEntity("查询成功",flowFromService.query(flowFromEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param flowFromEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("flowable:flowFrom:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) FlowFromEntity flowFromEntity){
        try {
            PageHelper.startPage(flowFromEntity.getPageNo(),flowFromEntity.getPageSize());
            List<FlowFromEntity> flowFromList = flowFromService.query(flowFromEntity);
            PageInfo<FlowFromEntity> pageInfo = new PageInfo<FlowFromEntity>(flowFromList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param flowFromEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("flowable:flowFrom:insert")
    public RespEntity insert(@RequestBody(required = false) FlowFromEntity flowFromEntity){
        try {
            flowFromEntity.setFfCreateTime(new Date());
            flowFromEntity.setFfCreateUser(this.getCurrentUser().getUserId());
            flowFromService.insert(flowFromEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param flowFromEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("flowable:flowFrom:update")
    public RespEntity update(@RequestBody(required = false) FlowFromEntity flowFromEntity){
        try {
            flowFromEntity.setFfUpdateTime(new Date());
            flowFromEntity.setFfUpdateUser(this.getCurrentUser().getUserId());
            flowFromService.update(flowFromEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param flowFromEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("flowable:flowFrom:delete")
    public RespEntity delete(@RequestBody(required = false) FlowFromEntity flowFromEntity){
        try {
            if(flowFromEntity.getFfId() == null){
                return new RespEntity("删除失败");
            }
            flowFromEntity.setFfUpdateTime(new Date());
            flowFromEntity.setFfUpdateUser(this.getCurrentUser().getUserId());
            flowFromService.logicalDelete(flowFromEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("删除失败");
        }
    }

    /**
     * 根据id查询
     * @param flowFromEntity
     * @return
     */
    @PostMapping("/getById")
    @RequiresPermissions("flowable:flowFrom:getById")
    public RespEntity getById(@RequestBody(required = false) FlowFromEntity flowFromEntity){
        try {
            if(flowFromEntity.getFfId() == null){
                return new RespEntity("请输入id");
            }
            return new RespEntity("查询成功",flowFromService.getById(flowFromEntity.getFfId()));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
     * 根据实体查询
     * @param flowFromEntity
     * @return
     */
    @PostMapping("/get")
    @RequiresPermissions("flowable:flowFrom:get")
    public RespEntity get(@RequestBody(required = false) FlowFromEntity flowFromEntity){
        try {
            return new RespEntity("查询成功",flowFromService.get(flowFromEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

}